﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace sand_game_core
{
    public class Player
    {
        Texture2D[] climb, jump, run, throwing, walk;
        Texture2D crouching, standing, currentFrame;
        public Vector2 position;
        int hSpeed, vSpeed;
        public int frameNo, counter;
        public Player(ContentManager content)
        {
            hSpeed = 0;
            vSpeed = 0;
            counter = 10;
            position = new Vector2(200, 300);
            crouching = content.Load<Texture2D>("crouching");
            standing = content.Load<Texture2D>("standing");
            climb = new Texture2D[6];
            run = new Texture2D[6];
            throwing = new Texture2D[6];
            walk = new Texture2D[6];
            for (int i = 0; i < 6; i++)
            {
                climb[i] = content.Load<Texture2D>("climb000" + i);
                run[i] = content.Load<Texture2D>("running000" + i);
                throwing[i] = content.Load<Texture2D>("throw000" + i);
                walk[i] = content.Load<Texture2D>("walk000" + i);
            }
            jump = new Texture2D[4];
            jump[0] = content.Load<Texture2D>("jump0000");
            jump[1] = content.Load<Texture2D>("jump0001");
            jump[2] = content.Load<Texture2D>("jump0002");
            jump[3] = content.Load<Texture2D>("jump0003");
        }
        public void draw(SpriteBatch s)
        {
            if (counter == 0)
            {
                frameNo++;
                counter = 5;
            }
            else
            {
                counter--;
            }
            if (frameNo == 6)
                frameNo = 0;
            currentFrame = getFrame();
            s.Draw(currentFrame, new Rectangle(
                (int)((position.X * (int)view.multiply) - (view.x * (int)view.multiply)),
                (int)((position.Y * (int)view.multiply + 1) - (view.y * (uint)view.multiply)),
                currentFrame.Width * (int)view.multiply / 2,
                currentFrame.Height * (int)view.multiply / 2), null, Color.White, 0, new Vector2(currentFrame.Width / 2, currentFrame.Height / 2), hSpeed < 0 ? SpriteEffects.FlipHorizontally : SpriteEffects.None, 1);
        }
        public Texture2D getFrame()
        {
            
            if (world.worldParticleLayer[(int)(position.X * world.arraywidth + (position.Y - jump[frameNo < 2 ? frameNo : 2].Height / 4-5))].myType == 0)
            {
                if (frameNo >= 3)
                    frameNo = 2;
                return jump[frameNo];
            }
            else if ((currentFrame == jump[2] || (currentFrame == jump[3] && vSpeed != 0))&& world.worldParticleLayer[(int)(position.X * world.arraywidth + (position.Y - jump[frameNo < 2 ? frameNo : 2].Height / 4))].myType != 0)
            {
                return jump[3];
            }
            else if (hSpeed == 0)
            {
                return Keyboard.GetState()[Keys.S] == KeyState.Down ? crouching : standing;
            }
            else
            {
                return Keyboard.GetState()[Keys.LeftShift] == KeyState.Down ? run[frameNo] : walk[frameNo];
            }
        }
        public void update(int height)
        {
            hSpeed += ((Keyboard.GetState()[Keys.A] == KeyState.Down && hSpeed > -(Keyboard.GetState()[Keys.LeftShift] == KeyState.Down ? 5 : 2)) || (hSpeed > 0 && !(Keyboard.GetState()[Keys.D] == KeyState.Down)) ? -1 : ((Keyboard.GetState()[Keys.D] == KeyState.Down && hSpeed < (Keyboard.GetState()[Keys.LeftShift] == KeyState.Down ? 5 : 2)) || (hSpeed < 0 && !(Keyboard.GetState()[Keys.A] == KeyState.Down)) ? 1 : 0));
            if (Keyboard.GetState()[Keys.W] == KeyState.Down && vSpeed < (Keyboard.GetState()[Keys.LeftShift] == KeyState.Down ? 10 : 5))
                vSpeed++;
            if (vSpeed > -5)
                vSpeed--;
            if (Keyboard.GetState()[Keys.Space] == KeyState.Down &&
                (
                world.worldParticleLayer[(int)(position.X * world.arraywidth + (position.Y - getFrame().Height / 4))].myType != 0))
            {
                vSpeed = 10;
                frameNo = 5;
            }
            position.X += position.X + hSpeed > 20 ? hSpeed : (hSpeed = 0);
            position.Y += position.Y + vSpeed > 100 || vSpeed > 0 ? (world.worldParticleLayer[(int)(position.X * world.arraywidth + (position.Y - getFrame().Height / 4 - 1))].myType == 0 ? vSpeed : vSpeed / 4) : (vSpeed = 0);

            while (world.worldParticleLayer[(int)(position.X * world.arraywidth + (position.Y - getFrame().Height / 4 + 1))].myType == 1 ||
                world.worldParticleLayer[(int)(position.X * world.arraywidth + (position.Y - getFrame().Height / 4 + 1))].myType == 2 ||
                world.worldParticleLayer[(int)(position.X * world.arraywidth + (position.Y - getFrame().Height / 4 + 1))].myType == 6 ||
                world.worldParticleLayer[(int)(position.X * world.arraywidth + (position.Y - getFrame().Height / 4 + 1))].myType == 7)
            {
                vSpeed = 0;
                position.Y++;
            }
        }

    }
}
